package zhuyun.code

fun main(args:Array<String>){
    val cities = setOf<String>("韶关","广州")
    val cityExpression = TerminalExpression(cities)
    val people = setOf<String>("老人","妇女","儿童")
    val personExpression = TerminalExpression(people)
    val cityPerson = AndExpression(cityExpression, personExpression)

    println(cityPerson.interpret("韶关的老人"))
    println(cityPerson.interpret("韶关的年轻人"))
    println(cityPerson.interpret("广州的妇女"))
    println(cityPerson.interpret("广州的儿童"))
    println(cityPerson.interpret("山东的儿童"))


}
interface Expression{
    fun interpret(info:String):Boolean
}
class TerminalExpression(val set:Set<String>): Expression {

    override fun interpret(info: String): Boolean {
        set.forEach {
            if(it.equals(info)){
                return true
            }
        }
        return false
    }

}

class AndExpression(val city: Expression, val person: Expression): Expression {
    override fun interpret(info: String): Boolean {
        val tokens = info.split("的")
        return city.interpret(tokens[0])&&person.interpret(tokens[1])
    }

}
